package SellTicket;



/*
[售票系统]，编程模拟三个售票窗口售票100分别使用继承Thread和实现Runnable方式，并分析有什么问题？
 */
public class Sell {
    public static void main(String[] args) {
//        new Sell01().start();
//        new Sell01().start();
//        new Sell01().start();
        new Thread(new Sell02()).start();
        new Thread(new Sell02()).start();
        new Thread(new Sell02()).start();
    }
}
//继承Thead方式
class Sell01 extends Thread {
    private static int TicketNum = 100 ;
    @Override
    public void run() {
        while(true) {
            if(TicketNum <= 0) {
                System.out.println("售票结束");
                break;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("窗口 "+Thread.currentThread().getName()+" 售出一张票"+" 剩余票数"+ (--TicketNum));
        }
    }
}
//实现Runnable方式
class Sell02 implements Runnable {
    private static int TicketNum = 100 ;
    @Override
    public void run() {
        while(true) {
            if(TicketNum <= 0) {
                System.out.println("售票结束");
                break;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("窗口 "+Thread.currentThread().getName()+" 售出一张票"+" 剩余票数"+ (--TicketNum));
        }
    }
}
//我们会发现运行结果出现了负数的情况，这就是问题所在。那么此时就引入了线程同步的概念，使用互斥锁还解决这个问题